package com.finance.rule.computer;

import com.alibaba.fastjson.JSONObject;
import com.finance.base.constant.BaseConstants;
import com.finance.base.enums.MajorTypeEnum;
import com.finance.base.exception.BaseException;
import com.finance.report.bean.pojo.LogisticsData;
import com.finance.report.service.ILogisticsDataService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 根据运单号的前缀
 */
@Component
@Scope("prototype")
public class WaybillNumPrefixAnalyzer implements IMajorAnalyzer ,Serializable {
    @Resource
    private ILogisticsDataService logisticsDataService;

    private String prefixWaybill = "";

    private String prefixBusinessbill = "";

    private String tableName = "";


    @Override
    public boolean isSingle(LogisticsData logisticsData) {
        String tableName = logisticsDataService.getDataTableName(logisticsData.getSignTime());
        if(logisticsData.getWaybillNum().startsWith(this.prefixWaybill)){
            List<LogisticsData> list = this.logisticsDataService.getByBusinessCode(logisticsData.getWaybillNum(),tableName);
            return CollectionUtils.isEmpty(list);
        }else if(logisticsData.getBusinessCode().startsWith(this.prefixBusinessbill)){
            LogisticsData majorLogisticsData = this.logisticsDataService.getByWaybillNum(tableName,logisticsData.getBusinessCode());
            return majorLogisticsData==null;
        }
        return true;
    }

    @Override
    public MajorTypeEnum majorTyeOf(LogisticsData logisticsData) {
        return logisticsData.getWaybillNum().startsWith(this.prefixWaybill)?MajorTypeEnum.MAJOR:MajorTypeEnum.DRAG;
    }

    @Override
    public List<LogisticsData> getOtherParts(LogisticsData logisticsData) {
        if(logisticsData.getWaybillNum().startsWith(this.prefixWaybill)){
            List<LogisticsData> list = this.logisticsDataService.getByBusinessCode(logisticsData.getWaybillNum(),tableName);
            return list;
        }else if(logisticsData.getBusinessCode().startsWith(this.prefixBusinessbill)){
            String tableName = this.logisticsDataService.getDataTableName(logisticsData.getSignTime());
            LogisticsData majorLogisticsData = this.logisticsDataService.getByWaybillNum(tableName,logisticsData.getBusinessCode());
            if(majorLogisticsData!=null){
                List<LogisticsData> list = this.logisticsDataService.getByBusinessCode(majorLogisticsData.getWaybillNum(),tableName);
                if(CollectionUtils.isEmpty(list)){
                    List<LogisticsData> list1 =new  ArrayList<LogisticsData>();
                    list1.add(majorLogisticsData);
                    return list1;
                }else{
                    list.remove(logisticsData);
                    return list;
                }
            }else{
                return new ArrayList<LogisticsData>();
            }
        }
        return new ArrayList<LogisticsData>();
    }

    @Override
    public void init(String json) {
        if (!StringUtils.isEmpty(json)) {
            JSONObject jsonObject = JSONObject.parseObject(json);
            prefixWaybill = jsonObject.getString("prefixWaybill");
            prefixBusinessbill = jsonObject.getString("prefixBusinessbill");
        } else {
            throw new BaseException(BaseConstants.ERR_INIT_JSON_IS_NULL);
        }
    }
}
